package demo_04;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseDao_01 {
    static final String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/jdbc";
    String user = "root";
    String pwd = "admin";
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rst = null;
    /**
     * static 代码块在类加载的时候执行，仅执行一次
     * 可以保证其中的代码只执行一次，防止驱动多次加载*/
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, pwd);
        return conn;
    }
    public void close() {
        if (rst != null) {
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rst = null;
            }
        }
        if (stmt != null) {
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stmt = null;
            }
        }
        if (conn != null) {
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }
    /**
     * 没有向外抛出异常，是为了释放数据库资源
     */
    public int executeUpdate(String sql, String... args) {
        int row = 0;
        try {
            conn = getConnection();
            //预编译sql语句
            stmt = conn.prepareStatement(sql);
            //args是jvm根据用户传入的数据自动创建的
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    stmt.setString(i + 1, args[i]);
                }
            }
            //执行sql语句
            row = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return row;
    }
    public List<Map<String, Object>> executeQuery(String sql, String... args) {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            conn = getConnection();
            stmt = conn.prepareStatement(sql);
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    stmt.setString(i + 1, args[i]);
                }
            }

            rst = stmt.executeQuery();
            ResultSetMetaData data = rst.getMetaData();
            int n = data.getColumnCount();
            //存放查询的列名，不是真正的列名，有可能是别名
            String[] names = new String[n];
            for (int i = 0; i < n; i++) {
                names[i] = data.getColumnLabel(i + 1);
            }
            while (rst.next()) {
                Map<String, Object> map = new HashMap<>();
                for(int i=0;i<n;i++){
                    String key = names[i];
                    Object value = rst.getObject(key);
                    map.put(key,value);

                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return list;
    }
}
